package com.bupt.fmmall.service.Impl;

import com.bupt.fmmall.dao.ShoppingCartDao;
import com.bupt.fmmall.entity.ShoppingCart;
import com.bupt.fmmall.entity.ShoppingCartVO;
import com.bupt.fmmall.service.ShoppingCartService;
import com.bupt.fmmall.vo.ResultVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    ShoppingCartDao shoppingCartDao;

    @Override
    public boolean add_to_Cart(ShoppingCart cart) {
        return false;
    }

    @Override
    public ResultVO findAllCartByUserId(String customeruserName) {
        List<ShoppingCartVO>list=shoppingCartDao.query_ShoppingCart_by_userName(customeruserName);
        return new ResultVO(200,"success",list);
    }

    @Override
    public ShoppingCart get_ShoppingCart_by_ShoppingCartId(int ShoppingCartId) {
        return null;
    }

    @Override
    public ResultVO delete_CartItem(Integer itemId,String customeruserName) {
        int i=shoppingCartDao.delete_ShoppingCart_product(itemId,customeruserName);
        if (i>0){
            return new ResultVO(200,"success",null);
        }else{
            return new ResultVO(601,"删除失败",null);
        }
    }

    @Override
    public boolean delete_all(String customeruserName) {
        return false;
    }

    @Override
    public ResultVO set_ShoppingCartId_number(Integer itemId,String customeruserName,Integer itemNumber) {
        int i=shoppingCartDao.update_ShoppingCart_productNum(itemId,customeruserName,itemNumber);
        if (i>0){
            return new ResultVO(200,"success",null);
        }else{
            return new ResultVO(601,"修改失败",null);
        }
    }

    @Override
    public ResultVO findChoosenCart(String itemId, String customeruserName) {
        //将前端传来的被选择的商品ID字符串转化成List<>
        String arr[]=itemId.split(",");
        List<Integer> itemIds=new ArrayList<>();
        for (int i=0;i<arr.length;i++){
            itemIds.add(Integer.parseInt(arr[i]));
        }
        List<ShoppingCartVO> list=shoppingCartDao.query_ShoppingCart_by_userNameAndItemid(itemIds,customeruserName);
        return new ResultVO(200,"success",list);
    }
}
